package top.mable.payment.listener;

import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import top.mable.common.vo.DemoMsg;

import java.io.IOException;
import java.util.HashMap;

/**
 * 延时队列，过期后，进入死信队列
 *
 * ObjectUtil.unserialize  ===》得到的對象需要实现  Serializable
 */
@Component
@Slf4j
public class MQDelayedMsgListener {


    /**
     * 延时队列绑定的  dead_direct_exchange 、dead.key
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = {@QueueBinding(exchange = @Exchange(value = "dead_direct_exchange",type = ExchangeTypes.TOPIC),
            value = @Queue(value = "dead.direct.queue",durable = Exchange.TRUE),
            key = "dead.key")})
    public void consumeDelayMsg(Message message, Channel channel) throws IOException {
        log.info("进入--MQDelayedMsgListener死信队列  {}",message.toString());
        DemoMsg unserialize = ObjectUtil.unserialize(message.getBody());
        log.info("反序列化的-MGg{}-",unserialize);
        //丢入另一个队列中
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }




}
